package com.scm.connection;

import java.io.IOException;
import org.apache.log4j.Logger;
import com.scm.property.PropertyConfig;
import com.scm.property.IPropertyConfig;
import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;


public class FtpConnection {
	
	private static Logger log = Logger.getLogger(FtpConnection.class);
	
	private final String FTP = "gnl.altervista.org ";
	private final String USER = "gnl";
	private final String PASS = "655946";	
	private final String DIR = "Session";
	
	public void updateSecurityKey(){
		    FTPClient client = new FTPClient();
			try {
				//BufferedWriter out = new BufferedWriter(new FileWriter("test.txt"));
				client.connect(FTP);
				client.login(USER, PASS);
				client.changeDirectory(DIR);
				IPropertyConfig property = new PropertyConfig();
				client.upload(new java.io.File(property.getStringFileSecret()));
				client.upload(new java.io.File(property.getStringFileToken()));
				log.info("Update OK!");
				client.disconnect(true);
			} catch (IllegalStateException e) {
				log.error("IllegalStateException",e);
			} catch (IOException e) {
				log.error("IOException",e);
			} catch (FTPIllegalReplyException e) {
				log.error("FTPIllegalReplyException",e);
			} catch (FTPException e) {
				log.error("FTPException",e);
			} catch (FTPDataTransferException e) {
				log.error("FTPDataTransferException",e);
			} catch (FTPAbortedException e) {
				log.error("FTPAbortedException",e);
			}
	}
	
	public void deleteSecurityKey(){
		   FTPClient client = new FTPClient();
					try {
						//BufferedWriter out = new BufferedWriter(new FileWriter("test.txt"));
						client.connect(FTP);
						client.login(USER, PASS);
						client.changeDirectory(DIR);
						String dir = client.currentDirectory();
						IPropertyConfig property = new PropertyConfig();
						client.deleteFile(property.getStringFileSecret());
						client.deleteFile(property.getStringFileToken());
						log.info("Delete OK!");
						client.disconnect(true);
					} catch (IllegalStateException e) {
						log.error("IllegalStateException",e);
					} catch (IOException e) {
						log.error("IOException",e);
					} catch (FTPIllegalReplyException e) {
						log.error("FTPIllegalReplyException",e);
					} catch (FTPException e) {
						log.error("FTPException",e);
					} 
		
	}

}
